#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

class logger:
	def __init__(self):
		self.logfile = open("LOG", "w")
	def dump(self, *data):
		line = "-".join(map(str, data))
		self.logfile.write(line + "\n")
	def __del__(self):
		self.logfile.close()
		
mylogger = logger()

def inventory(W, ms, cs, co, kmax):
	X = [ 0 for i in xrange(kmax+1) ]
	Xlast = 0
	Xant = 0
	U = [ 0 for i in xrange(kmax+1) ]
	cost = co
	lastbuy = 0
	U[0] = W[0]
	for k in xrange(1, kmax):
		dif = W[k] - Xant
		if (dif > 0):
			if (co > dif * cs * (k - lastbuy + 1) and (dif + Xlast <= ms)):
				cost += dif * cs
				U[lastbuy] += dif
				Xlast += dif
				Xant += dif
			else:
				cost += co
				lastbuy = k
				U[k] = dif
		Xant = Xant + U[k] - W[k]
		assert Xant <= ms, "problema con los X[k], alguno fue mal calculado"
		#mylogger.dump("iteracion {}, dif = {}, X[{}] = {}".format(k, dif,k, X[k]))
	#mylogger.dump(U,X,cost)
	return U, cost
	
def format_input(inp):
	fdesc = open(inp, "r")
	lines = fdesc.readlines()
	fdesc.close()
	elements = map(lambda s: s.replace("\n",""), filter(lambda s: s != "\n", lines))
	n = int(elements.pop(0))
	s = int(elements.pop(0))
	c = int(elements.pop(0))
	k = int(elements.pop(0))
	W = []
	for line in elements:
		W.append(int(line))
	return n,s,c,k,W
	
def main():
	sys.argv.pop(0)
	if len(sys.argv) != 1:
		sys.exit(1,"uso incorrecto del scrip. Uso correcto: ./inventory.py ARCHIVODATOS")
	n = s = c = k = W = None
	try:
		n, s, c, k, W = format_input(sys.argv.pop(0))
	except:
		sys.exit(1, "error en el archivo de datos")
	U,costo  = inventory(W,s,c,k,n)

#	for i,compra in enumerate(J):
#		print "J en el mes " + str(i) + ":" + str(compra)
	for i,compra in enumerate(U):
		print "compra en el mes " + str(i) + ":" + str(compra)
	print "costo total: " + str(costo)
	return 0

if __name__ == '__main__':
	main()

